package com.treeliked.netdiskdemo.service.impl;

import com.treeliked.netdiskdemo.model.Response;
import com.treeliked.netdiskdemo.model.ResultCode;
import com.treeliked.netdiskdemo.model.dao.FileMapper;
import com.treeliked.netdiskdemo.service.FileService;
import com.treeliked.netdiskdemo.util.FileUtils;
import com.treeliked.netdiskdemo.util.IdUtils;
import com.treeliked.netdiskdemo.util.OssUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 文件服务实现
 *
 * @author lqs2
 * @date 2018/11/29, Thu
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FileServiceImpl implements FileService {

    /**
     * mapper dao
     */
    private final FileMapper fileMapper;


    @Autowired
    public FileServiceImpl(FileMapper fileMapper) {
        this.fileMapper = fileMapper;
    }

    @Override
    public void saveFile(MultipartFile file, String path, String author, Response response) throws IOException {
        String name = file.getOriginalFilename();
        assert name != null;
        String suffix = FileUtils.getFileSuffixThroughName(name);
        long size = file.getSize();
        byte[] bytes = file.getBytes();

        String fileId = IdUtils.get32Id();
        // 使用oss上传文件
        boolean succeed = OssUtils.uploadFile(fileId + "." + suffix, file.getSize(), bytes);

        if (succeed) {
            // 插入一条数据库记录
            com.treeliked.netdiskdemo.model.dataobject.File storageFile = new com.treeliked.netdiskdemo.model.dataobject.File();
            storageFile.setId(fileId);
            storageFile.setAuthor(author);
            storageFile.setName(name);
            storageFile.setSuffix(suffix);
            storageFile.setSize(size);
            storageFile.setIsFile(true);
            storageFile.setDir(path);

            // 返回结果
            response.setCode(ResultCode.SUCCESS);
            response.setMessage("文件【" + name + "】上传成功");

            fileMapper.insert(storageFile);
        } else {
            response.setCode(ResultCode.FAIL);
            response.setMessage("文件【" + name + "】上传失败");
        }
    }

    @Override
    public void downloadFile(InputStream ips, OutputStream ops) throws Exception {
        IOUtils.copy(ips, ops);
        ips.close();
        ops.close();
    }

    @Override
    public com.treeliked.netdiskdemo.model.dataobject.File getFileById(String id) throws Exception {
        return fileMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteFileById(String id) throws Exception {
        com.treeliked.netdiskdemo.model.dataobject.File file = fileMapper.selectByPrimaryKey(id);
        String ossKey = file.getId() + "." + file.getSuffix();

        // 删除oss文件
        OssUtils.dropFile(ossKey);

        fileMapper.deleteByPrimaryKey(id);
    }

}
